/**
 * Project Name:zbj-real-name-mobile-web
 * File Name:ErrorController.java
 * Package com.zhubajie.real.name.mobile.controller
 * Date:2015-10-17下午4:27:20
 * Copyright (c) 2015 All Rights Reserved.
 *
 */
package com.inoneone.youba.mcht.controller;

import com.inoneone.youba.mcht.dto.BaseRsp;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.youba.mcht.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

/**
 * 
 * ClassName: ErrorController <br/>
 * Function: 错误异常统一处理. <br/>
 * date: 2016年8月31日 下午8:29:31 <br/>
 *
 * @author 姬远玄
 * @version
 * @since JDK 1.7
 */
@ControllerAdvice
public class ErrorController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public Object processUnauthenticatedException(NativeWebRequest request, Exception e) {
		logger.error("请求出现异常:", e);
		if (e instanceof BusinessRuntimeException) {
			BusinessRuntimeException bexc = (BusinessRuntimeException) e;

			BaseRsp baseRsp = new BaseRsp();
			baseRsp.setResultCode(bexc.getErrCode());
			if(CommonUtil.isNullOrEmpty(bexc.getAttach()))
            {
                baseRsp.setResultMsg(bexc.getErrMsg());
            }
            else
            {
                baseRsp.setResultMsg(bexc.getErrMsg() + " ," + bexc.getAttach());
            }
			return baseRsp;
		} else {
			BaseRsp baseRsp = new BaseRsp();
			baseRsp.setResultCode(ErrorCode.SYS_ERR.getErrCode());
			baseRsp.setResultMsg(ErrorCode.SYS_ERR.getErrMsg());
			return baseRsp;
		}
	}
}
